import * as nodemailer from 'nodemailer';

interface Mail {
  from: string; //发送人邮箱
  to: string; //接收人邮箱
  subject: string; //标题
  text?: string;
  html?: string;
}
export default class Email {
  config: any;
  transporter: any;
  constructor() {
    this.config = {
      host: 'smtp.163.com',
      port: 25,
      auth: {
        user: 'aliangnice@163.com', //刚才注册的邮箱账号
        pass: 'LCNJVZTPQGQRBFDV', //邮箱的授权码，不是注册时的密码
      },
    };
    this.transporter = nodemailer.createTransport(this.config);
  }

  send(mail: Mail) {
    return new Promise((resolve, reject) => {
      this.transporter.sendMail(mail, function (error, info) {
        console.info(info);
        if (error) {
          console.log(error);
          reject(error);
        }
        resolve(true);
      });
    });
  }
}
